#include <stdio.h>

int main( )
{
    int t, y, n, p;
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d%d", &y, &n);
        p = y & 3;
        if ( p == 0 ) p = 4;
        y = y + 4 - p;
        if ( y % 100 == 0 ) y += 4;
        while ( --n )
        {
            y += 4;
            if ( y % 100 == 0  && y % 400 != 0 )
                n++;
        }
        printf("%d\n", y);
    }
    return 0;
}
